package com.fenghongzhang.module_home.ui.recommend

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.fenghongzhang.lib_common.utils.GlideApp
import com.fenghongzhang.lib_common.utils.ImageLoader
import com.fenghongzhang.module_home.R
import com.fenghongzhang.module_home.databinding.HomeVideoCardItemLayoutBinding
import com.fenghongzhang.module_home.model.RecommendMutilVideo

class RecommendAdapter : ListAdapter<RecommendMutilVideo, RecommendAdapter.RecommendViewHolder>(DiffCallback()) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecommendViewHolder {
        val inflater = LayoutInflater.from(parent.context)
        val binding = HomeVideoCardItemLayoutBinding.inflate(inflater, parent, false)
        return RecommendViewHolder(binding)
    }

    override fun onBindViewHolder(holder: RecommendViewHolder, position: Int) {
        val item = getItem(position)
        holder.bind(item)
    }

    class RecommendViewHolder(private val binding: HomeVideoCardItemLayoutBinding) :
        RecyclerView.ViewHolder(binding.root) {

        fun bind(item: RecommendMutilVideo) {
            // 在这里绑定数据到视图
            // 例如：binding.videoTitle.text = item.title
            binding.videoTitle.text = item.title
            binding.videoInfo.text = item.favoritecount.toString()
            binding.videoDuration.text = item.duration

            ImageLoader.loadImage(binding.root.context,item.videomainimag,binding.videoThumbnail)

        }
    }

    class DiffCallback : DiffUtil.ItemCallback<RecommendMutilVideo>() {
        override fun areItemsTheSame(
            oldItem: RecommendMutilVideo,
            newItem: RecommendMutilVideo
        ): Boolean {
            return   oldItem == newItem
        }

        override fun areContentsTheSame(
            oldItem: RecommendMutilVideo,
            newItem: RecommendMutilVideo
        ): Boolean {
            return   oldItem == newItem
        }

    }
}